package com.focusai.efairy.ui.activity.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.focusai.efairy.R;
import com.focusai.efairy.model.request.ChangUserInfoRequest;
import com.focusai.efairy.model.user.UpLoadUserInfo;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.network.request.base.Request;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;

/**
 * Created by luo_guangwen on 2017/8/17.
 */

public class UserChangeNameActivity extends SwipeBackBaseActivity {

    public static final String EDIT_TYPE = "EDIT_TYPE";
    public static final String USER_INFO_ENTRY = "USER_INFO_ENTRY";
    public static final int EDIT_NAME = 0x100;
    public static final int EDIT_COMPANY = 0x101;
    public static final int EDIT_COMPANY_ADD = 0x102;
    public static final int EDIT_COMPANY_EMAIL = 0x103;
    private EditText nameEdit;
    private int type;
    private UpLoadUserInfo upLoadUserInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_user_change_name);
        initView();
        initdata();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add(MENU_GROUP_ID, MENU_ITEM_FILTER, 0, R.string.save).setTitle(R.string.save);
        MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void initView() {
        nameEdit = findView(R.id.name_edit);
    }


    public void initdata() {
        type = getIntent().getIntExtra(EDIT_TYPE, 0);
        upLoadUserInfo = (UpLoadUserInfo) getIntent().getSerializableExtra(USER_INFO_ENTRY);
        if (upLoadUserInfo == null || type == 0) {
            showToast("编辑失败");
            finish();
            return;
        }
        String title = getResources().getString(R.string.change_nickname);
        switch (type) {
            case EDIT_NAME:
                nameEdit.setText(upLoadUserInfo.getEfairyuser_nickname());
                break;
            case EDIT_COMPANY:
                title = "修改单位名称";
                nameEdit.setText(upLoadUserInfo.getEfairyuser_company());

                break;
            case EDIT_COMPANY_ADD:
                title = "修改单位地址";
                nameEdit.setText(upLoadUserInfo.getEfairyuser_company_address());

                break;
            case EDIT_COMPANY_EMAIL:
                title = "修改联系邮箱";
                nameEdit.setText(upLoadUserInfo.getEfairyuser_email());

                break;
            default:
                break;
        }
        setCenterTitleName(title);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == MENU_ITEM_FILTER) {
            setResult(RESULT_OK);
            showSingleDialog("正在保存修改..");
            upLoadUserName(nameEdit.getText().toString());
        }
        return super.onOptionsItemSelected(item);
    }

    private void upLoadUserName(String name) {
        switch (type) {
            case EDIT_NAME:
                upLoadUserInfo.setEfairyuser_nickname(name);
                break;
            case EDIT_COMPANY:
                upLoadUserInfo.setEfairyuser_company(name);
                break;
            case EDIT_COMPANY_ADD:
                upLoadUserInfo.setEfairyuser_company_address(name);
                break;
            case EDIT_COMPANY_EMAIL:
                upLoadUserInfo.setEfairyuser_email(name);
                break;
            default:
                break;
        }
        Request request = new ChangUserInfoRequest(upLoadUserInfo, new Response.Listener<Integer>() {
            @Override
            protected void onSuccess(Integer response) {
                closeSingleDialog();
                if (response == 200) {
                    showToast("保存成功");
                    Intent intent = new Intent();
                    intent.putExtra(USER_INFO_ENTRY,upLoadUserInfo);
                    setResult(RESULT_OK,intent);
                    finish();
                }
            }


            @Override
            protected void onFail(NetworkException exception) {
                closeSingleDialog();
            }
        });
        HttpManager.getInstance().sendRequest(request);
    }
}
